﻿"use strict";
define(['admin/app', "service/admin/orderService", "service/admin/logisticsService", "service/baseService"], function (app) {
    app.controller('orderController', function ($scope, $stateParams, $location, $uibModal, $state, utils, orderService, logisticsService, baseService) {
        $scope.query = {
            page: 1, size: 20, q: '', start_time: '', end_time: '', merchant_no: '', order_no: '', order_status: '', order_type: ''
        };
        $scope.goods = { sku: '', start_time: '', end_time: '', skus: [] };
        $scope.logistic_info = {};
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                orderService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            get: function () {
                if ($stateParams.id > 0) {
                    orderService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                        methods.get_shipping();
                    });
                    $scope.breadcrumb = '详情';
                }
            },
            get_shipping: function () {
                orderService.ashipping({ order_id: $stateParams.id }).success(function (response) {
                    $scope.logistic_info = response.data;
                });
            },
            goBack: function () {
                history.back();
            },
            status_name: function (status) {
                if (status == 1) return "订单受理";
                else if (status == 2) return "订单分配海外仓";
                else if (status == 3) return "批次确认准备预申报";
                else if (status == 4) return "口岸进口预申报完成";
                else if (status == 5) return "空运航班飞离始发港";
                else if (status == 6) return "身份信息匹配";
                else if (status == 7) return "空运航班抵达目的地";
                else if (status == 8) return "到库分栋，开始清关";
                else if (status == 9) return "清关完成开始配送";
                else if (status == 10) return "国内物流";
                else return "异常";
            },
            acceptance: function (id) {
                logisticsService.get({ id: id }).success(function (response) {
                    utils.notify(response.result_msg, "success");
                });
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            get_logistics: function () {
                orderService.getlogistics().success(function (response) {
                    if (response.result_code == 1000)
                        $scope.logistics = response.data;
                });
            },
            save_shipping: function () {
                var dto = {
                    order_no: $scope.model.order.order_no,
                    ship_order_number: $scope.model.order.ship_order_number
                };
                orderService.saveshipping({ dto: dto }).success(function (response) {
                    if (response.result_code == 1000) {
                        utils.notify(response.data, "success");
                    }
                    methods.goBack();
                });
            },
            recipient: function (id) {
                orderService.saverecipient({ id: id }).success(function (response) {
                    if (response.result_code == 1000) {
                        utils.notify(response.data, "success");
                    }
                    methods.get_orders();
                });
            },
            stop: function (id) {
                var modal = utils.confirm({
                    msg: "是否停止自动推单",
                    ok: "确定",
                    cancel: "取消"
                });
                modal.result.then(function () {
                    orderService.stop({ id: id }).success(function (response) {
                        if (response.result_code == 1000) {
                            utils.notify(response.data, "success");
                        }
                        methods.get_orders();
                    });
                });
            },
            save_info: function () {
                var dto = {
                    id: $stateParams.id,
                    real_name: $scope.model.order.shipping.real_name,
                    id_card: $scope.model.order.shipping.id_card,
                    region_name: $scope.model.order.shipping.region_name
                };
                orderService.saveinfo({ dto: dto }).success(function (response) {
                    if (response.result_code == 1000) {
                        utils.notify(response.data, "success");
                    }
                });
            },
            search_sku: function (sku) {
                orderService.searchsku({ sku: sku }).success(function (response) {
                    if (response.result_code == 1000) {
                        utils.notify(response.result_msg, "success");
                    }
                    $scope.goods = response.data;
                });
            },
            save_sku: function () {
                orderService.savesku($scope.goods).success(function (response) {
                    if (response.result_code == 1000) {
                        utils.notify(response.data, "success");
                    } else {
                        utils.notify(response.result_msg, "warning");
                    }
                });
            },
            save_order: function () {
                var dto = {
                    order_no: $scope.model.order.order_no,
                    new_order_no: $scope.model.order.new_order_no
                };
                orderService.saveorder(dto).success(function (response) {
                    if (response.result_code == 1000) {
                        utils.notify(response.data, "success");
                    } else {
                        utils.notify(response.result_msg, "warning");
                    }
                });
            },
            push: function (order_no) {
                orderService.push({ order_no: order_no }).success(function (response) {
                    if (response.Result.result_code == 1000) {
                        utils.notify(response.Result.data, "success");
                        methods.goBack();
                    } else {
                        utils.notify(response.Result.result_msg, "warning");
                    }
                });
            },
            get_order_status: function (status) {
                //:待付款1,待发货2,已完成3,已关闭4，待确认5
                if (status == 1)
                    return "待付款";
                else if (status == 2)
                    return "待发货";
                else if (status == 3)
                    return "待确认";
                else if (status == 4)
                    return "已完成";
                else if (status == 5)
                    return "已关闭";
                else
                    return "异常";
            },
            get_push_status: function (status) {
                if (status == 1)
                    return "未推送";
                else if (status == 2)
                    return "已推送";
                else if (status == 3)
                    return "推送失败";
                else
                    return "已处理";
            },
            order_delivery: function (id) {
                orderService.orderdelivery({ id: id }).success(function (response) {
                    if (response.result_code == 1000) {
                        utils.notify(response.data, "success");
                    }
                    window.location.reload();
                });
            },
            get_orders: function () {
                orderService.getorders($scope.query).success(function (response) {
                    $scope.model = response.data;
                    methods.get_shipping();
                });
            },
            page: function (page) {
                $scope.query.page = page;
                // methods.get_orders('page', $scope.query.page);
                $location.search('page', $scope.query.page);
            },
            kdniao_change: function (select_item, bind_model) {
                $scope.kdniao_selected = bind_model;
            },
            shipping: function () {
                if ($scope.kdniao_selected.value == undefined) {
                    utils.notify('请选择快递物流', "warning");
                    return;
                }
                if ($scope.model.ship_order_number == undefined) {
                    utils.notify('请填写快递单号', "warning");
                    return;
                }
                $scope.psdisabled = true;
                let order = {
                    order_no: $scope.model.order.order_no,
                    express_no: $scope.kdniao_selected.value,
                    express_company_name: $scope.kdniao_selected.text,
                    ship_order_number: $scope.model.ship_order_number
                }
                orderService.shipping(order).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        $scope.model.shipping.ship_order_number = $scope.model.ship_order_number;
                        $scope.model.shipping.express_companyname = $scope.kdniao_selected.text;
                        $scope.model.order_status = 3;
                    }
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
                history.back();
            },
            export: function () {
                $("#export").attr("href", "/order/export?start_time=" + $scope.query.start_time + "&end_time=" + $scope.query.end_time + "&order_status=" + $scope.query.order_status);
            },
            openImportDelivery: function (model) {
                $uibModal.open({
                    templateUrl: 'import_delivery.html',
                    controller: 'import_delivery_ctrl',
                    resolve: {
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.get_orders();
                });
            },
        }

        angular.extend($scope, methods);
        if ($state.includes('app.order.detail')) {
            baseService.dictionary().success(function (response) {
                $scope.kdniao = JSON.parse(response.data);
            });
            methods.get_logistics();
            methods.get();
        }
        else if ($state.includes('app.order.token_list')) {
            methods.get_orders();
        }
        else {
            methods.search();
        }
    });

    app.controller('import_delivery_ctrl', function ($scope, $uibModalInstance, utils, orderService, model) {
        $scope.model = model;
        $scope.importDelivery = function () {
            $scope.psdisabled = true;
            orderService.importDelivery($scope.model).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close($scope.model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
});